package com.parking.common.core.util;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * BelongsProject: eco-rebirth
 * BelongsPackage: com.ecology.commonscore.util
 * Author: panlonghu
 * CreateTime: 2023-10-17  16:24
 * Description: TODO
 * Version: 1.0
 */
public class DateUtil {



    /**
     *字符串转时间戳
     * @param yearMonthDay1 年月日年月日时分秒
     * @return
     * @throws Exception
     */
    public static Long stringToLong1(String yearMonthDay1)throws Exception{
        System.out.println(yearMonthDay1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long timeStr = sdf.parse(yearMonthDay1).getTime();
        return timeStr;
    }

    /**
     *字符串转时间戳
     * @param yearMonthDay 年月日
     * @return
     * @throws Exception
     */
    public static Long stringToLong(String yearMonthDay)throws Exception{
        System.out.println(yearMonthDay);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Long timeStr = sdf.parse(yearMonthDay).getTime();
        return timeStr;
    }

    /**
     *字符串转年月日
     * @param yearMonthDay 年月日
     * @return localDate
     */
    public static LocalDate stringToLocalDate(String yearMonthDay){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate = LocalDate.parse(yearMonthDay, dateTimeFormatter);
        return localDate;
    }

    /**
     * 字符串转年月日时分秒
     * @param time
     * @return
     */
    public static LocalDateTime stringToLocalDateTime(String time){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse(time, dateTimeFormatter);
        return localDateTime;
    }

    public static LocalDateTime stringToLocalDateTime(String time,String pattern){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        LocalDateTime localDateTime = LocalDateTime.parse(time, dateTimeFormatter);
        return localDateTime;
    }

    public static String localDateTimeToString(LocalDateTime time){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String localDateTime = time.format(dateTimeFormatter);
        return localDateTime;
    }

    public static String localDateTimeToString(LocalDateTime time, String pattern){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return time.format(dateTimeFormatter);
    }

    public static String localDateToString(LocalDate time, String pattern){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return time.format(dateTimeFormatter);
    }

    public static String localDateToString(LocalDate time){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return time.format(dateTimeFormatter);
    }

    //前端传日期格式解析
    public static LocalDateTime getLocalDateTime(String dateTimeString){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        DateTimeFormatter formatterOne = DateTimeFormatter.ofPattern("yyyy/MM/d HH:mm:ss");
        DateTimeFormatter formatterTwo = DateTimeFormatter.ofPattern("yyyy/M/d HH:mm:ss");
        DateTimeFormatter formatterThree = DateTimeFormatter.ofPattern("yyyy/M/dd HH:mm:ss");
        try {
            LocalDateTime localDateTime = LocalDateTime.parse(dateTimeString, dateTimeFormatter);
            return localDateTime;
        }catch (Exception e){
            try {
                LocalDateTime localDateTime = LocalDateTime.parse(dateTimeString, formatterOne);
                return localDateTime;
            }catch (Exception e1){
                try {
                    LocalDateTime localDateTime = LocalDateTime.parse(dateTimeString, formatterTwo);
                    return localDateTime;
                }catch (Exception e2){
                    LocalDateTime localDateTime = LocalDateTime.parse(dateTimeString, formatterThree);
                    return localDateTime;
                }
            }
        }

    }

    public static void main(String[] args) throws Exception{
//        String s = "2023-11-15";
//        String s = "2023-11-15 11:20:20";
//        Long aLong = DateUtil.stringToLong(s);
//        System.out.println(aLong);

        Long aLong = DateUtil.stringToLong1("2023-12-14 17:48:12");
        System.out.println(aLong);

        Date date1 = new Date();
        System.out.println("当前时间："+date1);
        Date date2 = new Date(date1.getTime()+(1000*60*60*24));
        System.out.println("明天的当前时间："+date2);

        System.out.println(date1.getTime());
        System.out.println(date2.getTime());


//        String starDate = s.substring(0, 10);
//        String starTime = s.substring(11, 19);
//        System.out.println(starDate);
//        System.out.println(starTime);
    }

}
